package com.web.logindemo.controller;

import com.web.logindemo.common.Constants;
import com.web.logindemo.common.util.IDUtils;
import com.web.logindemo.config.RedisCache;
import com.web.logindemo.domain.AjaxResult;
import com.web.logindemo.domain.LoginBody;
import com.web.logindemo.service.LoginService;
import com.google.code.kaptcha.Producer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.FastByteArrayOutputStream;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

@RestController
public class LoginController {


    @Resource(name = "captchaProducer")
    private Producer captchaProducer;

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private LoginService loginService;

    @ResponseBody
    @GetMapping("hello")
    public String hello() {
        return "hello world";
    }


    @ResponseBody
    @GetMapping("index")
    public String index() {
        return "恭喜你通过验证了！";
    }


    /**
     * 用户登录
     *
     * @param loginBody
     * @return
     */
    @PostMapping("login")
    public AjaxResult login(@RequestBody LoginBody loginBody) {
        AjaxResult ajaxResult = AjaxResult.success();
        //生成令牌
        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(),
                loginBody.getCode(), loginBody.getUuid());
        ajaxResult.put(Constants.TOKEN, token);
        return ajaxResult;
    }

    /**
     * 生成验证码
     */
    @GetMapping("captchaImage")
    public AjaxResult getCode(HttpServletRequest request) throws IOException {
        AjaxResult ajax = new AjaxResult();

        //保存验证码信息
        String uuid = IDUtils.simpleUUID();
        String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;


        //生成验证码
        String capStr = null, code = null;
        BufferedImage image = null;
        capStr = code = captchaProducer.createText();
        image = captchaProducer.createImage(capStr);

        //验证码存入redis
        redisCache.setCacheObject(verifyKey, code, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);

        //转换流信息写出
        FastByteArrayOutputStream os = new FastByteArrayOutputStream();
        try {
            ImageIO.write(image, "jpg", os);
        } catch (IOException e) {
            return AjaxResult.error(e.getMessage());
        }
        ajax.put("uuid", uuid);
        ajax.put("code", code);
//        ajax.put("image", Base64.encode(os.toByteArray()));
        return ajax;
    }

    /**
     * 查询缓存中的code
     */
    @ResponseBody
    @GetMapping("getRedisCode")
    public AjaxResult getRedisCode(String uuid) {
        String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
        String code = redisCache.getCacheObject(verifyKey);
        return new AjaxResult(200, "success", code);
    }
}
